<?php

namespace api\components;

use api\components\dataCenter\DataCenterSearchModel;
use api\components\dataCenter\ExportToCsv;
use api\components\dataCenter\QueryTrait;
use api\components\dataCenter\SearchFilterBuilder;
use api\models\AsyncDownloadTask;
use api\modules\app\models\MetaArea;
use api\utils\ArrayUtil;
use api\utils\StringUtil;
use app\components\dataCenter\models\AreaEvent;
use Exception;
use Yii;
use yii\helpers\ArrayHelper;
use yii\helpers\Json;

class DataCenter
{
    use QueryTrait;

    public $host;
    public $token;

    const API_PATH_GENERIC_QUERY = '/united-ciimc-api/v1/generic-query';

    public function __construct()
    {
        if (isset(Yii::$app->params['data_center']['host'])) {
            $this->host = Yii::$app->params['data_center']['host'];
        }
        if (isset(Yii::$app->params['data_center']['token'])) {
            $this->token = Yii::$app->params['data_center']['token'];
        }
    }

    private function ttt($url, $method, $params = [], $body = [], $isJson = false)
    {
        $method = strtolower($method);
        $params = array_merge($params, ['token' => $this->token]);
        $url = $url . '?' . http_build_query($params);
        echo $url;exit;
        if ($method === 'get') {
            $response = $this->request($url, 'GET');
        } else {
            $response = $this->request($url, 'POST', $body, $isJson);
        }

        $response = json_decode($response, true);
        return $response;
    }

    private function _request($url, $method, $params = [], $body = [], $isJson = false)
    {
        $method = strtolower($method);
        $params = array_merge($params, ['token' => $this->token]);
        $url = $url . '?' . http_build_query($params);
        if ($method === 'get') {
            $response = $this->request($url, 'GET');
        } else {
            $response = $this->request($url, 'POST', $body, $isJson);
        }

        $response = json_decode($response, true);
        return $response;
    }

    public function request(string $url, string $method = "get", $params = [], $isJson = false)
    {
        $method = strtolower($method);
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        if ($method == 'post') {
            if ($isJson) {
                $params = is_string($params) ? $params : Json::encode($params);
                curl_setopt($curl, CURLOPT_HTTPHEADER, array(
                    'Content-Type: application/json',
                    'Content-Length: ' . strlen($params),
                ));
            }
            curl_setopt($curl, CURLOPT_POST, 1); //post提交内容
            curl_setopt($curl, CURLOPT_POSTFIELDS, $params);
        }
        $response = curl_exec($curl);
        return $response;
    }

    public function getGenericQuery($params) {
        // $base_url = $this->host . self::API_PATH_GENERIC_QUERY;
        $base_url = DATA_CENTER_HOST . self::API_PATH_GENERIC_QUERY;
        $response = $this->_request($base_url, 'get', $params);
        if (ArrayHelper::getValue($response, 'result') !== 'success') {
            $message = ArrayHelper::getValue($response, 'result');
            Yii::error('error_message:' . $message);
        }
        return $response;
    }

    //x经度 y纬度 lat纬度 lng经度
    //wgs84 gcj02 shlocal
    //目前支持：84-城地，84-高德，高德-84，城地-84四种转换方式。
    public function coordinateTransform($coordX,$coordY,$originalType='shlocal',$transformType='wgs84') {
        $url = DATA_CENTER_HOST . "/united-ciimc-api/v1/coordinate-transform";

//        $url = "http://10.210.232.238/united-ciimc-api/v1/coordinate-transform";
//$this->token = '240e13b16eff42aeaec9bda8c5707f84';

        $params = [
            'coordx' => $coordX,
            'coordy' => $coordY,
            'original_type' => $originalType,
            'transform_type' => $transformType,
        ];
        $response = $this->_request($url, 'get', $params);
\Yii::error('coordinateTransform======'.json_encode([$url,$params,$response]));
        if (ArrayHelper::getValue($response, 'result') !== 'success') {
            $message = ArrayHelper::getValue($response, 'result');
            Yii::error('coordinateTransform:' . $message);
        }
        return $response;
    }
}
